package com.aaa.controller;


import com.aaa.entity.FrontUser;
import com.aaa.service.IFrontUserService;
import com.aaa.service.impl.FrontUserServiceImpl;
import com.aaa.util.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Bean
 * @since 2021-09-02
 */
@Api(tags = "前台用户接口")
@RestController
@CrossOrigin
@RequestMapping("/front-user")
public class FrontUserController {
    @Autowired
    FrontUserServiceImpl frontUserService;



    @ApiOperation("登录")
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public List<Map<String,Object>> login(String username,String password)
    {
        return frontUserService.login(username,password);
    }

    @ApiOperation("注册")
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Integer add(@ApiParam("头像")@RequestPart("photo")MultipartFile photo,String uname,String password,String phone,Integer sex) throws IOException {
        String upload = FileUtil.upload(photo, "D:/idea/queick/" + uname + "/head/");
        String head="";
        if (upload!=""){
             head="D:/idea/queick/"+uname+"/head/"+upload;
        }
        return frontUserService.add(uname,password,head,phone,sex);
    }


    /**
     * 自己的用户主页信息或者是他人的主页用户信息
     *
     * @param uid 当查询自己的用过户主页需要传入 uid
     * @param id 当查询其他人的用户主页需要传入  自己id（uid）  被查看者id（id）    未登录时只传入被查看人id（id）
     * @return
     */
    @ApiOperation("用户主页信息")
    @RequestMapping(value = "seleuser",method = RequestMethod.POST)
    public List<Map<String,Object>> seleuser(@ApiParam("用户自己id") Integer uid,@ApiParam("被查看者id,查看自己主页不传") Integer id){
        return frontUserService.selectuser(uid,id);
    }
    /**
     * 查询用户的所有粉丝
     * @param uid
     * @return
     */
    @ApiOperation("查看所有粉丝")
    @RequestMapping(value = "fans",method = RequestMethod.POST)
    public List<Map<String,Object>> fans(@ApiParam("用户id") Integer uid){
        return frontUserService.fansuser(uid);
    }

    /***
     * 查询用户的所有关注人数
     * @param uid
     * @return
     */
    @ApiOperation("用户的所有关注人")
    @RequestMapping(value = "atten",method = RequestMethod.POST)
    public List<Map<String,Object>> atten(@ApiParam("用户id") Integer uid){
        return frontUserService.attenuser(uid);
    }

    /***
     *修改个人信息
     * @param photo
     * @param frontUser
     * @return
     */
    @ApiOperation("修改用户信息")
    @RequestMapping(value = "user_upda",method = RequestMethod.POST)
    public boolean user_upda(@ApiParam("头像")@RequestPart("photo")MultipartFile photo,FrontUser frontUser) throws IOException {
        String upload = FileUtil.upload(photo, "D:/idea/queick/" + frontUser.getUname() + "/head/");
        if(""!=upload){
            String head="/"+frontUser.getUname()+"/head/"+upload;
            frontUser.setPhoto(head);
        }
        return frontUserService.updateById(frontUser);
    }
    @ApiOperation("关闭发布订阅")
    @RequestMapping(value = "closepush",method = RequestMethod.POST)
    public boolean closepush(@ApiParam("用户主键") Integer uid){
        FrontUser frontUser = new FrontUser();
        frontUser.setUid(uid);
        frontUser.setPush(1);
       return frontUserService.updateById(frontUser);
    }

    @ApiOperation("查询消息列表")
    @RequestMapping(value = "inform",method = RequestMethod.POST)
    public List<Map<String,Object>> seleinform(@ApiParam("用户id") Integer uid){
        return frontUserService.seleinform(uid);
    }

}
